#ifndef __MY_UTIL__
#define  __MY_UTIL__

/*
实用工具类设计：
    1.获取系统时间
    2.判断文件是否存在
    3.获取文件所在路径
    4.创建文件目录
*/
#include <iostream>
#include <ctime>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>

namespace iceLogs
{
    namespace util
    {
        
        class Date
        {
            public:
                static time_t getTime()
                {
                    return time(nullptr);
                }
        };

        class File
        {
            public:
                static bool exists(const std::string &pathname)
                {
                    struct stat st;
                    if(stat(pathname.c_str(), &st) == 0)//判断文件是否存在
                    {
                        return true;
                    }
                    return false;
                    //return (access(pathname.c_str(), F_OK) == 0);//系统调用接口，跨平台移植性差，但是简单
                }

                static std::string path(const std::string &pathname)
                {
                    size_t pos = pathname.find_last_of("/\\");
                    if(pos == std::string::npos)
                    {
                        return ".";
                    }
                    return pathname.substr(0, pos + 1);
                }

                static void createDirectory(const std::string &pathname)
                {
                    size_t pos = 0, idx = 0;
                    while (idx < pathname.size())
                    {
                        pos = pathname.find_first_of("/\\", idx);
                        if (pos == std::string::npos)
                        {
                            mkdir(pathname.c_str(), 0777);
                            return;
                        }

                        std::string parentname = pathname.substr(0, pos + 1);

                        if (exists(parentname) == true)
                        {
                            idx = pos + 1;
                            continue;
                        }
                        mkdir(parentname.c_str(), 0777);
                        idx = pos + 1;
                    }
                }
        };


    }
}

#endif